package com.attendance.www.hr.service.impl;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.OrganizationMainBean;
import com.attendance.www.hr.dao.mapper.CorporationExtMapper;
import com.attendance.www.hr.dao.mapper.CorporationMapper;
import com.attendance.www.hr.dao.mapper.DepartmentExtMapper;
import com.attendance.www.hr.dao.mapper.DepartmentMapper;
import com.attendance.www.hr.dao.mapper.EmployeeCorporationMapper;
import com.attendance.www.hr.dao.mapper.MainOrganizationsExtMapper;
import com.attendance.www.hr.dao.mapper.MainOrganizationsMapper;
import com.attendance.www.hr.dao.mapper.MainOrganizationsNewExtMapper;
import com.attendance.www.hr.entitys.Corporation;
import com.attendance.www.hr.entitys.CorporationExample;
import com.attendance.www.hr.entitys.Department;
import com.attendance.www.hr.entitys.DepartmentExample;
import com.attendance.www.hr.service.IOrganizationsMainService;

@Service  
public class OrganizationsMainServiceImpl implements IOrganizationsMainService {
	@Resource
	private CorporationMapper corporationDao;
	@Resource 
	private CorporationExtMapper corporationExtDao;
	@Resource
	private DepartmentMapper departmentDao;
	@Resource
	private DepartmentExtMapper departmentExtDao;
	@Resource
	private EmployeeCorporationMapper employeeCorporationDao;
	@Resource
	private MainOrganizationsMapper mainOrganizationsDao;
	@Resource
	private MainOrganizationsExtMapper mainOrganizationsExtDao;
	@Resource
	private MainOrganizationsNewExtMapper mainOrganizationsNewExtDao;
	
	/**
	 * 同步公司和部门数据
	 * @param corporationMainBeanList 同步数据集合
	 * @param inType 类型：1 全量 2 增量
	 */
	@Override
	public void saveOrganizationsMain(List<OrganizationMainBean> organizationMainBeanList,Integer inType){
		Date corporationFromDate=null;
		Date departmentFromDate=null;
		//查询公司集合
		CorporationExample corporationExample=new CorporationExample();
		corporationExample.createCriteria().andIsDeleteEqualTo(0);
		List<Corporation> crporationList=corporationDao.selectByExample(corporationExample);
		LinkedHashMap<String, Corporation> crporationMap = new LinkedHashMap<String, Corporation>();
		for(int ci=0;ci<crporationList.size();ci++) {
			crporationMap.put(crporationList.get(ci).getCorporationCode(), crporationList.get(ci));
		}
		corporationFromDate=corporationExtDao.selectCorporationMinFromDate();
		
		//查询部门集合
		DepartmentExample departmentExample=new DepartmentExample();
		departmentExample.createCriteria().andIsDisableEqualTo(0);
		List<Department> departmentList=departmentDao.selectByExample(departmentExample);
		LinkedHashMap<String, Department> departmentMap = new LinkedHashMap<String, Department>();
		for(int di=0;di<departmentList.size();di++) {
			departmentMap.put(departmentList.get(di).getDepartmentCode(), departmentList.get(di));
		}
		departmentFromDate=departmentExtDao.selectDepartmentMinFromDate();
		
		//判断机构类型是否为公司
		//Company为公司 
		for (int i = 0; i < organizationMainBeanList.size(); i++) {
			OrganizationMainBean organizationMainBean=organizationMainBeanList.get(i);
			if(organizationMainBean.getOrganType().equals("Company")) {
				Corporation corporation=crporationMap.get(organizationMainBean.getOrganCode());
				if(corporation!=null) {
					//修改
					corporation.setCorporationCode(organizationMainBean.getOrganCode());
					corporation.setCorporationName(organizationMainBean.getOrganName());
					corporation.setDescription(organizationMainBean.getRemark());
					corporation.setFromDate(corporationFromDate);
					if(StringUtils.isNotBlank(organizationMainBean.getOpenStatus())){
						if(organizationMainBean.getOpenStatus().equals("2") || organizationMainBean.getOpenStatus().equals("3")) {
							corporation.setIsDelete(1);
						}else if(organizationMainBean.getOpenStatus().equals("1")) {
							corporation.setIsDelete(0);
						}
					}
					//操作区分
					if(StringUtils.isNotBlank(organizationMainBean.getActionFlag())) {
						if(organizationMainBean.getActionFlag().equals("2") || organizationMainBean.getActionFlag().equals("6")) {
							corporation.setIsDelete(1);
						}else if(organizationMainBean.getActionFlag().equals("3")) {
							corporation.setIsDelete(0);
						}
					}
					corporation.setUpdateTime(new Date());
					corporationDao.updateByPrimaryKey(corporation);
				}else {
					//增加
					corporation=new Corporation();
					corporation.setCorporationCode(organizationMainBean.getOrganCode());
					corporation.setCorporationName(organizationMainBean.getOrganName());
					corporation.setDescription(organizationMainBean.getRemark());
					corporation.setFromDate(corporationFromDate);
					if(StringUtils.isNotBlank(organizationMainBean.getOpenStatus())){
						if(organizationMainBean.getOpenStatus().equals("2") || organizationMainBean.getOpenStatus().equals("3")) {
							corporation.setIsDelete(1);
						}else if(organizationMainBean.getOpenStatus().equals("1")) {
							corporation.setIsDelete(0);
						}
					}
					//操作区分
					if(StringUtils.isNotBlank(organizationMainBean.getActionFlag())) {
						if(organizationMainBean.getActionFlag().equals("2") || organizationMainBean.getActionFlag().equals("6")) {
							corporation.setIsDelete(1);
						}else if(organizationMainBean.getActionFlag().equals("3")) {
							corporation.setIsDelete(0);
						}
					}
					corporationDao.insertSelective(corporation);
					//添加
					crporationMap.put(corporation.getCorporationCode(),corporation);
				}
			}
		}
		
		//判断机构类型是否为部门
		this.setOrganizationMain(organizationMainBeanList, inType, departmentMap, departmentFromDate, crporationMap);
		//因数据层级不规律，所以处理两次
		this.setOrganizationMain(organizationMainBeanList, inType, departmentMap, departmentFromDate, crporationMap);
	}
	
	/**
	 * 处理部门数据
	 * @param organizationMainBeanList
	 * @param inType
	 * @param departmentMap
	 * @param departmentFromDate
	 * @param crporationMap
	 */
	private void setOrganizationMain(List<OrganizationMainBean> organizationMainBeanList,
									 Integer inType,
									 LinkedHashMap<String, Department> departmentMap,
									 Date departmentFromDate,
									 LinkedHashMap<String, Corporation> crporationMap) {
		//判断机构类型是否为部门
		//Depart为部门
		for (int i = 0; i < organizationMainBeanList.size(); i++) {
			OrganizationMainBean organizationMainBean=organizationMainBeanList.get(i);
			if(organizationMainBean.getOrganType().equals("Depart")) {
				Department department=departmentMap.get(organizationMainBean.getOrganCode());
				if(department!=null) {
					//修改
					department.setDepartmentCode(organizationMainBean.getOrganCode());
					department.setDepartmentName(organizationMainBean.getOrganName());
					department.setDescription(organizationMainBean.getRemark());
					department.setFromDate(departmentFromDate);
					
					//判断上级部门编码是否为公司或部门
					if(StringUtils.isNotBlank(organizationMainBean.getSuperiorCode())) {
						//如果为公司，存储公司编码
						Corporation corporation=crporationMap.get(organizationMainBean.getSuperiorCode());
						if(corporation!=null) {
							department.setCorporationId(corporation.getCorporationId());
						}else {
							//如果为部门，存储部门编码，并查询上级部门中的公司ID
							Department depMap=departmentMap.get(organizationMainBean.getSuperiorCode());
							if(depMap!=null) {
								department.setParentDepartmentId(depMap.getDepartmentId());
								//递归查询公司编码
								String crporationCode=null;
								if(inType==1) {
									crporationCode=mainOrganizationsExtDao.selectSuperiorCodeByOrganCode(organizationMainBean.getSuperiorCode());
								}else if(inType==2) {
									crporationCode=mainOrganizationsNewExtDao.selectSuperiorCodeNewByOrganCode(organizationMainBean.getSuperiorCode());
								}
								corporation=crporationMap.get(crporationCode);
								if(corporation!=null) {
									department.setCorporationId(corporation.getCorporationId());
								}else {
									//如果为空-表示为增量后该部门变更上级部门-通过上级部门编码查询对应部门表获取部门对应公司ID
									Department depCopMap=departmentMap.get(crporationCode);
									if(depCopMap!=null) {
										department.setCorporationId(depCopMap.getCorporationId());
									}
								}
							}
						}
					}
					
					//启用状态
					if(StringUtils.isNotBlank(organizationMainBean.getOpenStatus())) {
						if(organizationMainBean.getOpenStatus().equals("2") || organizationMainBean.getOpenStatus().equals("3")) {
							department.setIsDisable(1);
							department.setDisableTime(new Date());
						}else {
							department.setIsDisable(0);
						}
					}
					
					//操作区分
					if(StringUtils.isNotBlank(organizationMainBean.getActionFlag())) {
						if(organizationMainBean.getActionFlag().equals("2") || organizationMainBean.getActionFlag().equals("6")) {
							department.setIsDisable(1);
							department.setDisableTime(new Date());
						}else if(organizationMainBean.getActionFlag().equals("3")) {
							department.setIsDisable(0);
						}
					}
					
					department.setUpdateTime(new Date());
					
					//公司ID不等于空，在执行操作
					if(department.getCorporationId()!=null) {
						departmentDao.updateByPrimaryKey(department);
						if(department.getIsDisable()==0) {
							departmentExtDao.updateSetDisableTimeByDepartmentId(department.getDepartmentId());
						}
					}
				}else {
					//增加
					department=new Department(); 
					department.setDepartmentCode(organizationMainBean.getOrganCode());
					department.setDepartmentName(organizationMainBean.getOrganName());
					department.setDescription(organizationMainBean.getRemark());
					department.setFromDate(departmentFromDate);
					
					//判断上级部门编码是否为公司或部门
					if(StringUtils.isNotBlank(organizationMainBean.getSuperiorCode())) {
						//如果为公司，存储公司编码
						Corporation corporation=crporationMap.get(organizationMainBean.getSuperiorCode());
						if(corporation!=null) {
							department.setCorporationId(corporation.getCorporationId());
						}else {
							//如果为部门，存储部门编码，并查询上级部门中的公司ID
							Department depMap=departmentMap.get(organizationMainBean.getSuperiorCode());
							if(depMap!=null) {
								department.setParentDepartmentId(depMap.getDepartmentId());
								//递归查询公司编码
								String crporationCode=null;
								if(inType==1) {
									crporationCode=mainOrganizationsExtDao.selectSuperiorCodeByOrganCode(organizationMainBean.getSuperiorCode());
								}else if(inType==2) {
									crporationCode=mainOrganizationsNewExtDao.selectSuperiorCodeNewByOrganCode(organizationMainBean.getSuperiorCode());
								}
								corporation=crporationMap.get(crporationCode);
								if(corporation!=null) {
									department.setCorporationId(corporation.getCorporationId());
								}else {
									//如果为空-表示为增量后该部门变更上级部门-通过上级部门编码查询对应部门表获取部门对应公司ID
									Department depCopMap=departmentMap.get(crporationCode);
									if(depCopMap!=null) {
										department.setCorporationId(depCopMap.getCorporationId());
									}
								}
							}
						}
					}
					
					//启用状态
					if(StringUtils.isNotBlank(organizationMainBean.getOpenStatus())){
						if(organizationMainBean.getOpenStatus().equals("2")  || organizationMainBean.getOpenStatus().equals("3")) {
							department.setIsDisable(1);
							department.setDisableTime(new Date());
						}else {
							department.setIsDisable(0);
						}
					}
					
					//操作区分
					if(StringUtils.isNotBlank(organizationMainBean.getActionFlag())) {
						if(organizationMainBean.getActionFlag().equals("2") || organizationMainBean.getActionFlag().equals("6")) {
							department.setIsDisable(1);
							department.setDisableTime(new Date());
						}else if(organizationMainBean.getActionFlag().equals("3")) {
							department.setIsDisable(0);
						}
					}
					//公司ID不等于空，在执行操作
					if(department.getCorporationId()!=null) {
						//添加
						departmentDao.insertSelective(department);
						if(department.getIsDisable()==0) {
							departmentExtDao.updateSetDisableTimeByDepartmentId(department.getDepartmentId());
						}
					}
					departmentMap.put(department.getDepartmentCode(),department);
				}
			}
		}
	}
	
}

